package com.hunter.api.http;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

import com.hunter.api.bean.EntityBean;
import com.hunter.api.handler.EntityHandler;
import com.hunter.api.handler.annotation.UserLoginToken;
import com.hunter.api.help.UserHelper;
import com.hunter.api.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@Transactional(propagation = Propagation.REQUIRED)
public class EntityHttp {
    @Autowired
    private EntityHandler entityHandler;

    @Autowired
    UserHelper userHelper;

    @Autowired
    private UserMapper userMapper;

    @ResponseBody
    @UserLoginToken
    @RequestMapping(value ="ent/listView")
    public JSONObject EntityLv(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        entity.setData(userHelper.getUserInfo(httpServletRequest, entity.getData()));
        json = entityHandler.EntityLv(entity);
        return json;

    }


    @UserLoginToken
    @ResponseBody
    @RequestMapping("ent/info")
    public JSONObject EntityInfo(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            entity.setData(this.userHelper.getUserInfo(httpServletRequest, entity.getData()));
            json = entityHandler.EntityInfo(entity);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code", 500);
            json.put("msg", e.toString());
            json.put("data","{}" );
            return json;
        }
        return json;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping("ent/create")
    public JSONObject EntityCreate(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try{
            entity.setData(this.userHelper.getUserInfo(httpServletRequest, entity.getData()));
            json = entityHandler.Entity(entity,"insert");
        }catch (Exception e){
            e.printStackTrace();
            json.put("msg", e.toString());
            json.put("data","{}" );
            return json;
        }
        return json;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping("ent/update")
    public JSONObject EntityUpdate(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try{
            entity.setData(this.userHelper.getUserInfo(httpServletRequest, entity.getData()));
            json = entityHandler.Entity(entity,"update");
        }catch (Exception e){
            e.printStackTrace();
            json.put("msg", e.toString());
            json.put("data","{}" );
            return json;
        }
        return json;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping("ent/delete")
    public JSONObject EntityDelete(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            entity.setData(this.userHelper.getUserInfo(httpServletRequest, entity.getData()));
            json = entityHandler.Entity(entity, "delete");
        }catch (Exception e){
            e.printStackTrace();
            json.put("code", 500);
            json.put("msg", e.toString());
            json.put("data","{}" );
            return json;
        }
        return json;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping("ent/enable")
    public JSONObject EntityEnable(@RequestBody EntityBean entity, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            entity.setData(this.userHelper.getUserInfo(httpServletRequest, entity.getData()));
            json = entityHandler.Entity(entity, "enable");
        }catch (Exception e){
            e.printStackTrace();
            json.put("code", 500);
            json.put("msg", e.toString());
            json.put("data","{}" );
            return json;
        }
        return json;
    }
}
